/*
 * Copyright (c) 2018-2999 深圳市阿毛科技有限公司 All rights reserved.
 *
 * https://www.lamyz.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.lam.mall.api.controller;


import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lam.common.base.controller.BaseController;
import com.lam.common.base.domain.Result;
import com.lam.framework.operLog.annotation.Log;
import com.lam.mall.app.vo.HotSearchVo;
import com.lam.mall.app.vo.SearchProdVo;
import com.lam.mall.service.HotSearchService;
import com.lam.mall.service.ProductService;

import cn.hutool.core.collection.CollectionUtil;

/**
 * @author lam
 */
@RestController
@RequestMapping("/${frontPath}/search")
@Log(title = "搜索接口")
public class SearchApiController extends BaseController{
	@Autowired
    private HotSearchService hotSearchService;
    @Autowired
    private ProductService productService;

    
    /**
     * 
     * 根据店铺id,热搜数量获取热搜
     * @param shopId
     * @param number
     * @param sort
     * @return
     */
//    @Parameter(name = "shopId", description = "店铺id" , required = true),
//    @Parameter(name = "number", description = "取数" , required = true),
//    @Parameter(name = "sort", description = "是否按照顺序(0 否 1是)"),
    @GetMapping("/hotSearchByShopId")
    @Log(oper = "查看店铺热搜")
    public Result hotSearchByShopId(String shopId,Integer number,Integer sort) {
    	
        List<HotSearchVo> list = hotSearchService.getCacheByShopId(shopId);
        return getListResponseEntity(number, sort, list);
    }

    /**
     * 根据店铺id,热搜数量获取热搜
     * @param number
     * @param sort
     * @return
     */
//    @Parameter(name = "number", description = "取数" , required = true),
//    @Parameter(name = "sort", description = "是否按照顺序(0 否 1是)", required = false ),
    @GetMapping("/hotSearch")
    @Log(oper = "查看全局热搜")
    public Result hotSearch(Integer number,Integer sort) {
        List<HotSearchVo> list = hotSearchService.getCacheByShopId("0");
        return getListResponseEntity(number, sort, list);
    }

    private Result getListResponseEntity(Integer number, Integer sort, List<HotSearchVo> list) {
        if(sort == null || sort == 0){
            Collections.shuffle(list);
        }
        if(!CollectionUtil.isNotEmpty(list) || list.size()< number){
            return Result.ok(list);
        }
        return Result.ok(list.subList(0, number));
    }

//    @Parameter(name = "prodName", description = "商品名" , required = true),
//    @Parameter(name = "sort", description = "排序(0 默认排序 1销量排序 2价格排序)"),
//    @Parameter(name = "orderBy", description = "排序(0升序 1降序)"),
//    @Parameter(name = "shopId", description = "店铺id" , required = true),
    @GetMapping("/searchProdPage")
    @Log(oper = "分页搜索商品")
    public Result searchProdPage(String prodName, Integer sort, Integer orderBy, String shopId) {

    	startPage();
    	List<SearchProdVo> list = productService.getSearchProdPageByProdName(prodName,sort,orderBy);
        return Result.ok(getTableData(list));
    }




}
